package cn.hospital.controller;

import cn.hospital.entity.*;
import cn.hospital.service.FoGroupService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/foGroup")
public class FoGroupController {

    @Resource
    private FoGroupService foGroupService;


    //-------------------------------------------------------------------------单个随访组管理-----------------------------------------------
    /**
     * 查询随访组成员信息
     * @param diagnosis
     * @param patientInformationSheet
     * @param foGroup
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String selectFoGroupList(Diagnosis diagnosis, PatientInformationSheet patientInformationSheet,
                                    FoGroup foGroup, Model model) {
        List<FoGroup> foGroupList = foGroupService.selectFoGroupList(diagnosis, patientInformationSheet, foGroup);
        List<Fo2GroupId> fo2GroupIdList = foGroupService.selectFo2GroupList(foGroup);
        model.addAttribute("foGroupList", foGroupList);
        model.addAttribute("fo2GroupIdList", fo2GroupIdList);
        model.addAttribute("diagnosis",diagnosis);
        model.addAttribute("patientInformationSheet",patientInformationSheet);
        model.addAttribute("foGroup",foGroup);
        return "page/grouping/onefollowUp";
    }

    /**
     * 随访员加入随访组
     *
     * @param model
     * @return
     */
    @RequestMapping("/addFogroupMeaningUser")
    private String selectMeaningUser(Model model, FoGroup foGroup, MeaningUser meaningUser) throws ParseException {
        if (meaningUser != null && foGroup != null) {
            if (meaningUser.getUserId() != null) {
                foGroupService.insertGroupMeaningUser(meaningUser, foGroup);
            }
        }
        List<MeaningUser> meaningUserList = foGroupService.selectMeaningUser(foGroup);
        model.addAttribute("meaningUserList", meaningUserList);
        model.addAttribute("foGroup", foGroup);
        return "page/grouping/addfoGroupMeaningUser";
    }
    /**
     * 病人加入随访组
     * @param model
     * @return
     */
    @RequestMapping("/addFogroupPatientInformationSheet")
    private String selectPatientInformationSheet(Model model, FoGroup foGroup, PatientInformationSheet patientInformationSheet) throws ParseException {
        if (patientInformationSheet != null && foGroup != null) {
            if (patientInformationSheet.getPatientId() != null) {
                foGroupService.insertGroupPatientInformationSheet(patientInformationSheet, foGroup);
            }
        }
        List<PatientInformationSheet> patientInformationSheetList = foGroupService.selectPatientInformationSheet(foGroup);
        model.addAttribute("patientInformationSheetList",patientInformationSheetList);
        model.addAttribute("foGroup", foGroup);
        return "page/grouping/addfoGroupPatientInformationSheet";
    }

    /**
     * 删除随访组成员信息--随访员
     *
     * @param userId
     * @return
     */
    @RequestMapping("/deletefo2GroupId")
    @ResponseBody
    private Integer deleteFogroupfo2GroupId(Integer[] userId) {
        Integer delete = foGroupService.deleteFoGroupfo2GroupId(userId);
        return delete;
    }

    /**
     * 删除随访组成员信息--病人
     *
     * @param patientId
     * @return
     */
    @RequestMapping("/deletefoGroupId")
    @ResponseBody
    private Integer deleteFogroupfoGroupId(Integer[] patientId) {
        Integer delete = foGroupService.deleteFoGroupfoGroupId(patientId);
        return delete;
    }

    /**
     * 查询模板进度管理数据
     */
    @RequestMapping("/selectFoGroup")
    @ResponseBody
    public Map<String, Object> selectFoGroup(String department, String group, String startDate, String endDate,
                                             String hospital, String name, String state, String pageNoStr, String pageSizeStr) {
        //获取参数
        long pageNo = 1;    //如果没有传数据,默认为第一页
        if (pageNoStr != null && pageNoStr.trim().length() > 0) {
            pageNo = Long.parseLong(pageNoStr);
        }
        int pageSize = 5;    //如果没有传数据,默认为10条数据
        if (pageSizeStr != null && pageSizeStr.trim().length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        long beginNo = (pageNo - 1) * pageSize;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("department", department);
        map.put("group", group);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("hospital", hospital);
        map.put("name", name);
        map.put("state", state);
        map.put("beginNo", beginNo);
        map.put("pageSize", pageSize);
        Map<String, Object> mapList = new HashMap<String, Object>();
        List<FoGroup> foGroups = foGroupService.queryFoGroup(map);
        Long countList = foGroupService.selectFoGroupCount(map);
        mapList.put("foGroupList", foGroups);
        mapList.put("countList", countList);
        return mapList;
    }
}
